package cn.com.shbs.Config;

import cn.com.shbs.model.BaseNcardSerive;
import net.sf.json.JSONObject;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;


public class CommonUtil {
    /**
     * 订单code前缀
     * PREFIX_SHOPCART
     */
    public static String PREFIX_GOODSORDER="O";
    /**
     * 步长
     * stepnum
     */
    public static int STEPNUM=1;

    //用户
    public static int SYSUSER_TYPE=1001;
    //客户
    public static int CUSTOMER_TYPE=1003;
    //次卡基本服务
    public static int NCARDSERVICE_TYPE=1016;
    //次卡
    public static int NCARD_TYPE=1017;
    //次卡购买记录code
    public static int NCARDPURCHASE_TYPE=1018;
    //次卡项目记录code
    public static int NCARDSER_TYPE=1019;
    //流水code
    public static int ACCOUNT_TYPE=1020;
    //服务人员表-提成表
    public static int SERVICEPEOPLE_TYPE=1021;

    //用户code
    //案件
    public static int CASE_TYPE=1004;
    // 被执行人
    public static int SUBJECTED_TYPE=1011;
    //文件
    public static int CODE_TYPE=1009;
    //文件
    public static int CODE_APPROVALID=1010;
    
    
   //TASK
    public static int TASK_TYPE=1005;
    public static int TASK_PROCESS=1006;
    public static int DOCUMENT_TYPE=1007;
    public static int MESSAGE_TYPE=1008;
    public static int LOGGER_TYPE=1011;
    public static int OPERATION_TYPE=1012;
    public static int RECEIVALES_TYPE=1013;
    public static int MODEL_TYPE=1014;
  //店铺
    public static int STORE_TYPE=1015;
    
    
    
    public static int EMPLOYEE_TYPE=1016;
    public static String PREFIX_SYSUSER="U";
    //顾客code
    public static String PREFIX_CUSTOMER="C";
    //次卡基本服务
    public static String PREFIX_NCARDSERVICE="NC";
    //次卡code
    public static String PREFIX_BASENCARD="BC";
    //次卡购买记录code
    public static String PREFIX_NCARDPURCHASE="NP";
    //次卡项目记录code
    public static String PREFIX_NCARD="N";
    //流水code
    public static String PREFIX_ACCOUNT="AS";
    //服务人员表-提成表code
    public static String PREFIX_SERVICEPEOPLE="SP";
    public static String PREFIX_DOCUMENT="D";
    public static String PREFIX_LOGGER="L";
    public static String PREFIX_TASK="T";
    public static String PREFIX_PROCESS="P";
    public static String PREFIX_MODEL="E";
    public static String PREFIX_CASE="A";
    public static String PREFIX_CODE="F";
    public static String PREFIX_APPROVALID="O";
    public static String PREFIX_MESSAGE="M";
    public static String PREFIX_OPERATION="O";
    public static String PREFIX_RECEIVALES="R";
    public static String PREFIX_SUBJECTED="S";
    public static String PREFIX_STORE="SP";//店铺
    //员工
    public static String PREFIX_EMPLOYEE = "EY";


    public static String getRandomSalt(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }


    
    /**
     * @Title: getNowTime_tamp
     * @Description:获取系统的当前时间
     * @author rqq
     * @return 返回 Timestamp 类型的时间
     */
    public static Timestamp getNowTime_tamp(){
        //获取当前时间
        Date now = new Date();
        Long tim = now.getTime();
        Timestamp time = new Timestamp(tim);
        return time;
    }

    public static String getPrintSize(long size) {
        //如果字节数少于1024，则直接以B为单位，否则先除于1024，后3位因太少无意义
        if (size < 1024) {
            return String.valueOf(size) + "B";
        } else {
            size = size / 1024;
        }
        //如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位
        //因为还没有到达要使用另一个单位的时候
        //接下去以此类推
        if (size < 1024) {
            return String.valueOf(size) + "KB";
        } else {
            size = size / 1024;
        }
        if (size < 1024) {
            //因为如果以MB为单位的话，要保留最后1位小数，
            //因此，把此数乘以100之后再取余
            size = size * 100;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "MB";
        } else {
            //否则如果要以GB为单位的，先除于1024再作同样的处理
            size = size * 100 / 1024;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "GB";

        }
    }

    /**
     * 通过时间秒毫秒数判断两个时间的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1,Date date2)
    {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
        return days;
    }

    public static String TimestamptoSting(Timestamp timestamp,String format){
        DateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(timestamp);
    }
    /**
     * 将一个 Map 对象转化为一个 JavaBean
     * @param type 要转化的类型
     * @param map 包含属性值的 map
     * @return 转化出来的 JavaBean 对象
     * @throws IntrospectionException 如果分析类属性失败
     * @throws IllegalAccessException 如果实例化 JavaBean 失败
     * @throws InstantiationException 如果实例化 JavaBean 失败
     * @throws InvocationTargetException 如果调用属性的 setter 方法失败
     */
    @SuppressWarnings("rawtypes")
    public static Object convertMap(Class type, Map map)
            throws IntrospectionException, IllegalAccessException,
            InstantiationException, InvocationTargetException {
        BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
        Object obj = type.newInstance(); // 创建 JavaBean 对象

        // 给 JavaBean 对象的属性赋值
        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
        for (int i = 0; i< propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();

            if (map.containsKey(propertyName)) {
                // 下面一句可以 try 起来，这样当一个属性赋值失败的时候就不会影响其他属性赋值。
                Object value = map.get(propertyName);
                if(value!=null){
                    Object[] args = new Object[1];
                    args[0] = value;
                    try {
                        descriptor.getWriteMethod().invoke(obj, args);
                    }catch (Exception e){
                        System.out.println("key:"+propertyName +"/val:"+value);
                    }


                }

            }
        }
        return obj;
    }

    /**
     * json转化成订单实体
     * @param jsonStr
     * @return
     */
    public static BaseNcardSerive JSON2Object(String jsonStr){
        //接收{}对象，此处接收数组对象会有异常
        if(jsonStr.indexOf("[") != -1){
            jsonStr = jsonStr.replace("[", "");
        }
        if(jsonStr.indexOf("]") != -1){
            jsonStr = jsonStr.replace("]", "");
        }
        JSONObject obj = new JSONObject().fromObject(jsonStr);//将json字符串转换为json对象
        BaseNcardSerive jb = (BaseNcardSerive)JSONObject.toBean(obj,BaseNcardSerive.class);//将建json对象转换为Person对象
        return jb;//返回一个Person对象
    }
}
